<?php


namespace app\api\service;

use app\common\model\article\ArticleCate;
use tmcore\base\BaseService;

class ArticleCateService extends BaseService
{
    /**
     * 获取栏目列表
     * @return array
     */
    public static function getList(): array
    {
        // 搜索条件
        $searchParam = [
            '=' => ['status'],
            '%like%' => ['name']
        ];
        $where = self::getSearchData($searchParam, []);

        $lists = ArticleCate::where($where)
            ->order(['sort' => 'desc', 'id' => 'desc'])
            ->append(['status_desc', 'article_count'])
            ->select()
            ->toArray();
        return linear_to_tree($lists, 'children');
    }

    /**
     * 查看文章栏目详情
     * @param $params
     * @return array
     */
    public static function detail($params) : array
    {
        return ArticleCate::findOrEmpty($params['id'])->toArray();
    }

    /**
     * 文章栏目数据
     * @return array
     */
    public static function getAllData()
    {
        $lists = ArticleCate::where(['status' => 1])
            ->field('id, pid, name')
            ->order(['sort' => 'desc', 'id' => 'desc'])
            ->select()
            ->toArray();
        return linear_to_tree($lists, 'children');
    }

}